home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- '''Fixer for print.
-
- Change:
- \'print\' into \'print()\'
- \'print ...\' into \'print(...)\'
- \'print ... ,\' into \'print(..., end=" ")\'
- \'print >>x, ...\' into \'print(..., file=x)\'
-
- No changes are applied if print_function is imported from __future__
-
- '''
- from import patcomp
- from import pytree
- from pgen2 import token
- from import fixer_base
- from fixer_util import Name, Call, Comma, String, is_tuple
- parend_expr = patcomp.compile_pattern("atom< '(' [atom|STRING|NAME] ')' >")
-
- class FixPrint(fixer_base.ConditionalFix):
- PATTERN = "\n simple_stmt< any* bare='print' any* > | print_stmt\n "
- skip_on = '__future__.print_function'
-
- def transform(self, node, results):
- if not results:
- raise AssertionError
- if self.should_skip(node):
- return None
- bare_print = results.get('bare')
- if bare_print:
- bare_print.replace(Call(Name('print'), [], prefix = bare_print.get_prefix()))
- return None
- if not node.children[0] == Name('print'):
- raise AssertionError
- args = node.children[1:]
- if len(args) == 1 and parend_expr.match(args[0]):
- return None
- sep = None
- end = None
- file = None
- l_args = [ arg.clone() for arg in args ]
- if sep is not None and end is not None or file is not None:
- if end is not None:
- self.add_kwarg(l_args, 'end', String(repr(end)))
-
- if file is not None:
- self.add_kwarg(l_args, 'file', file)
-
-
- n_stmt = Call(Name('print'), l_args)
- n_stmt.set_prefix(node.get_prefix())
- return n_stmt
-
-
- def add_kwarg(self, l_nodes, s_kwd, n_expr):
- n_expr.set_prefix('')
- n_argument = pytree.Node(self.syms.argument, (Name(s_kwd), pytree.Leaf(token.EQUAL, '='), n_expr))
- if l_nodes:
- l_nodes.append(Comma())
- n_argument.set_prefix(' ')
-
- l_nodes.append(n_argument)
-
-
-